using System;
using System.Collections;

namespace Commons.Models.Settings.Matchers.Operators.Relational
{
	[Serializable]
	public class LessThanOperator<T> : IRelationalOperator<T>
	{
		public virtual OperatorType Type
		{
			get
			{
				return OperatorType.Lt;
			}
		}

		public virtual bool Operate(T v1, T v2)
		{
			if (v1 == null)
			{
				return false;
			}
			return GetMinValue(v1) < GetMinValue(v2);
		}

		protected double GetMinValue(T v)
		{
			if (v is IEnumerable)
			{
				return GetMinValue((IEnumerable) v);
			}
			else
			{
				return RelationalOperatorSupport.GetDouble(v);
			}
		}

		protected double GetMinValue(IEnumerable list)
		{
			double minValue = double.MaxValue;
			foreach (object v in list)
			{
				minValue = Math.Min(RelationalOperatorSupport.GetDouble(v), minValue);
			}
			return minValue;
		}
	}
}

